    //
//  UnitViewController.m
//  Growth
//
//  Created by Hobo.Chen on 1/5/12.
//  Copyright 2012 MEETStudio. All rights reserved.
//

#import "UnitViewController.h"

@interface UnitViewController (Private)

- (CATransition *)animationWith:(ViewChangeAnimationType)type;

@end


@implementation UnitViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


#pragma mark -view animation

- (void)addNextViewWith:(UIViewController *)controller forView:(UIView *)forView animationType:(ViewChangeAnimationType)animationType insertIndex:(NSInteger)insertIndex
{
	
	CATransition *animation = [self animationWith:animationType];
	[forView.layer addAnimation:animation forKey:@"animation"];
	
	[forView insertSubview:controller.view atIndex:insertIndex];
	
	
}

- (void)addNextViewFlipWith:(UIViewController *)controller forView:(UIView *)forView isRight:(BOOL)isRight insertIndex:(NSInteger)insertIndex
{
	
	[UIView beginAnimations:@"animationID" context:nil];
	[UIView setAnimationDuration:kViewChangeAnimationDuration *2];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
	
	if (isRight) {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:forView cache:YES];
	}
	else {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:forView cache:YES];
	}
	
	[forView insertSubview:controller.view atIndex:insertIndex];
	
	[UIView commitAnimations];
	
}

- (void)removeViewWith:(UIViewController *)controller animationType:(ViewChangeAnimationType)animationType
{
	UIView *forView = [controller.view superview];
	
	CATransition *animation = [self animationWith:animationType];
	[forView.layer addAnimation:animation forKey:@"animation"];
	
	[controller.view removeFromSuperview];
	
	
}

- (void)removeViewFlipWith:(UIViewController *)controller isRight:(BOOL)isRight
{
	UIView *forView = [controller.view superview];
	
	[UIView beginAnimations:@"animationID" context:nil];
	[UIView setAnimationDuration:kViewChangeAnimationDuration *2];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
	
	if (isRight) {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:forView cache:YES];
	}
	else {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:forView cache:YES];
	}
	
	[controller.view removeFromSuperview];
	
	[UIView commitAnimations];
	
}



- (CATransition *)animationWith:(ViewChangeAnimationType)type
{
	CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = kViewChangeAnimationDuration;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.removedOnCompletion = NO;
	
	/*
	 kCATransitionFade;
	 kCATransitionMoveIn;
	 kCATransitionPush;
	 kCATransitionReveal;
	 */
	/*
	 kCATransitionFromRight;
	 kCATransitionFromLeft;
	 kCATransitionFromTop;
	 kCATransitionFromBottom;
	 */
	/*
	 kPushLeft,
	 kPushRight,
	 kPushUp,
	 kPushDown,
	 kFlipRight,
	 kFlipLeft,
	 */
	switch (type) {
		case kPushLeft:
			animation.type = kCATransitionMoveIn;
			animation.subtype = kCATransitionFromLeft;
			break;
		case kPushRight:
			animation.type = kCATransitionMoveIn;
			animation.subtype = kCATransitionFromRight;
			break;
		case kPushUp:
			animation.type = kCATransitionMoveIn;
			animation.subtype = kCATransitionFromTop;
			break;
		case kPushDown:
			animation.type = kCATransitionMoveIn;
			animation.subtype = kCATransitionFromBottom;
			break;
		default:
			break;
	}
	
	return animation;
	
}

@end
